package cate.game.res.hoard.addition;

import cate.common.table.res.hoard.row.HoardLevelRow;
import cate.common.table.res.hoard.row.HoardTaskRow;
import cate.common.table.res.hoard.row.HoardTypeRow;
import cate.game.attr.FightAttr;
import cate.game.res.hoard.HoardItem;
import cate.game.role.Role;
import cate.game.role.RoleDependent;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@NoteClass("灵界秘藏加成")
public class HoardAddition extends RoleDependent {
    @NoteField(value = "秘藏图鉴<原型ID，任务数据>")
    public Map<Integer, HoardItem> itemMap;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (itemMap == null) {
            itemMap = new HashMap<>();
        }

    }

    @JsonIgnore
    public FightAttr getAttr() {
        FightAttr attr = new FightAttr();
        int exp = role.getRes().hoard.hoardExp;
        HoardLevelRow hoardLevelRow = role.getGame().table.hoard.level.showLevel(exp);
        attr.add(new FightAttr(hoardLevelRow.reward));
        return attr;
    }

    @JsonIgnore
    public Map<Integer, FightAttr> getAttrMap() {
        Map<Integer, FightAttr> attrMap = new HashMap<>();
        for(int protoId : role.getRes().hoard.gotMap.keySet()){
            FightAttr attr = new FightAttr();
            HoardTypeRow hoardTypeRow = role.getGame().table.hoard.hoardType.showHoard(protoId, role.getRes().hoard.gotMap.get(protoId));
            int taskCount = itemMap.get(hoardTypeRow.protoId).addCount;
            //基础图鉴加成
            attr.add(new FightAttr(hoardTypeRow.attrStr));
            //任务次数加成
            for (int i = 0; i < taskCount; i++){
                attr.add(new FightAttr(hoardTypeRow.addAttrStr));
            }
            attrMap.put(protoId, attr);
        }
        return attrMap;
    }

    public boolean addTask(int taskId) {
        boolean ifHoard = false;
        if (!role.getRes().hoard.isOpen() || itemMap.isEmpty()) {
            return false;
        }
        HoardTaskRow taskRow = role.getGame().table.hoard.task.get(taskId);
        if (taskRow == null) {
            return false;
        }
        for(HoardItem item : itemMap.values()) {
            if (item.taskId == taskId) {
                HoardTypeRow row = role.getGame().table.hoard.hoardType.get(item.itemId);
                int max = row.numBase * row.numMax;
                if (max <= item.taskCount) {
                    return false;
                }
                item.taskCount ++;
                item.addCount = item.taskCount / row.numBase;
                if (taskRow.type == 2) {
                    int floor = role.getFarm().tower.passedFloor;
                    item.taskCount = Math.min(floor, max);
                    item.addCount = item.taskCount / row.numBase;
                }
                ifHoard = true;
                role.getRes().hoard.noticeUpdate();
            }
        }
        return ifHoard;
    }

}
